//
//  MyOrderMainController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/7/11.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#define TopViewHight 40

#import "MyOrderMainController.h"

#import "GoodsOrderViewController.h"
#import "OilOrderViewController.h"
#import "CoastOrderViewController.h"
#import "ActivityOrderViewController.h"

@interface MyOrderMainController ()<UIScrollViewDelegate>
{
    
}

@property(nonatomic,strong) UIView *topView;

@property(nonatomic,strong) UIView *slideView;

@property(nonatomic,strong) UIScrollView *bottomScrollView;

@property(nonatomic,strong) NSMutableArray *btnArray;

@property(nonatomic,strong) NSArray *vcArray;

@property(nonatomic,strong) UIButton *selectedButton;;

@end

@implementation MyOrderMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addScrollView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.view.backgroundColor = [UIColor whiteColor];

}

- (NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [[NSMutableArray alloc] initWithCapacity:4];
    }
    return _btnArray;
}
- (NSArray *)vcArray
{
    if (_vcArray == nil) {
        _vcArray = [[NSArray alloc] init];
        _vcArray = @[@{@"goods":[GoodsOrderViewController new]},@{@"oil":[OilOrderViewController new]},@{@"coast":[CoastOrderViewController new]},@{@"coast":[ActivityOrderViewController new]}];
    }
    return _vcArray;
}

- (void)addScrollView
{
    
    // 按钮宽度
    CGFloat buttonWight = WIDTH/self.vcArray.count;
    // 按钮高度
    CGFloat buttonhight = TopViewHight - 4;
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,WIDTH,TopViewHight)];
    [self.view addSubview:_topView];
    
    self.slideView = [[UIView alloc] initWithFrame:CGRectMake(0,TopViewHight-2, WIDTH/4, 2)];
    _slideView.backgroundColor = DEFAULT_COLOR;
    [_topView addSubview:_slideView];
    
    
    
    //按钮v
    NSArray *arr = @[@"商品",@"加油卡",@"话费",@"活动"];
    for (int i = 0; i < self.vcArray.count; ++i) {
        
        UIView *View = [[UIView alloc] initWithFrame:CGRectMake(buttonWight*i, 0,buttonWight, buttonhight)];
        
        UIButton *btn = [[UIButton alloc] init];;
        [btn setTitle:[NSString stringWithFormat:@"%@",arr[i]] forState:UIControlStateNormal];
        [btn setFrame:CGRectMake(0,0,buttonWight,buttonhight)];
        
        btn.tag = i;
        
        btn.titleLabel.font = FONT(15);
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:RGB(52, 52, 52) forState:UIControlStateNormal];
        [btn setTitleColor:DEFAULT_COLOR forState:UIControlStateDisabled];
        
        if (i==_scrollerIndex) {
            btn.enabled = false;
            self.selectedButton = btn;
        }
        
        [View addSubview:btn];
        [_topView addSubview:View];
        [self.btnArray addObject:btn];
        
        [btn addTarget:self action:@selector(topButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    //
    CGRect  bottomScrollViewFrame = CGRectMake(0,TopViewHight,WIDTH,HEIGHT - TopViewHight );
    self.bottomScrollView= [[UIScrollView alloc] initWithFrame:bottomScrollViewFrame];
    [self.view addSubview:_bottomScrollView];
    _bottomScrollView.contentSize = CGSizeMake(_vcArray.count*WIDTH,0);
    _bottomScrollView.pagingEnabled = YES;
    _bottomScrollView.showsVerticalScrollIndicator = false;
    _bottomScrollView.showsHorizontalScrollIndicator = false;
    _bottomScrollView.bounces = false;
    _bottomScrollView.directionalLockEnabled = YES;
    _bottomScrollView.delegate =self;
    
    //滚动位置
    _bottomScrollView.contentOffset = CGPointMake(WIDTH*_scrollerIndex, 0);
    
    for (int i = 0; i < self.vcArray.count ; i ++) {
        CGRect  VCFrame = CGRectMake(i * WIDTH, 0,WIDTH, bottomScrollViewFrame.size.height);
        
        NSString * key = [self.vcArray[i] allKeys][0];
        UIViewController * vc = _vcArray[i][key] ;
        vc.view.frame = VCFrame;
        
        [self.bottomScrollView addSubview:vc.view];
    }
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//
//    NSLog(@"_scrollerIndex %ld",_scrollerIndex);
//    NSLog(@"%ld",(long)scrollView.contentOffset.x);
//    NSLog(@" btnarr%ld",self.btnArray.count);
    
    CGRect frame = _slideView.frame;
    frame.origin.x = scrollView.contentOffset.x/self.vcArray.count;
    _slideView.frame = frame;
    int pageNum = scrollView.contentOffset.x / WIDTH;
//    NSLog(@"pageNum %d",pageNum);
    //滚动buttonLabel color
    for (UIButton * btn in self.btnArray) {
        if (btn.tag == pageNum ) {
            btn.enabled = false;
        }else{
            btn.enabled = YES;
        }
    }
}

- (void)topButtonDidClick:(id)sender
{
    UIButton *button = sender;  
    self.selectedButton.enabled = YES;
    button.enabled = false;
    self.selectedButton = button;
    
    [_bottomScrollView setContentOffset:CGPointMake(button.tag * WIDTH, 0) animated:YES];
}

@end

